package com.changxianggu.crawler.cloud.test;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.StrUtil;
import com.changxianggu.cloud.crawler.core.WebApplication;
import com.changxianggu.cloud.crawler.core.modules.storage.entity.CxgBook;
import com.changxianggu.cloud.crawler.core.modules.storage.service.ICxgBookService;
import com.changxianggu.cloud.crawler.core.utils.HttpUtil;
import com.changxianggu.cloud.crawler.core.utils.UserAgentUtil;
import lombok.extern.slf4j.Slf4j;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

@RunWith(SpringRunner.class)
@SpringBootTest(classes = {WebApplication.class})// 指定启动类
@Slf4j
public class HuaGongSheTuShuGuan {

    @Autowired
    ICxgBookService bookService;

    public static final String COOKIE = "ASP.NET_SessionId=fdgy2ti05v2bydhpb3ijwm5j; Hm_lvt_5a747bd786a9fce50db2b1570cf17632=1582187436,1582250113,1582275086; Hm_lpvt_5a747bd786a9fce50db2b1570cf17632=1582507675; .ASPXAUTH=CAA60237671F14EA973AE7606AC06E3418BF1AB8139DACC31A09F4094F337A2A9559DD6BFCFCD99DA9AC538295F2749910FBFD10A5BA24FF226EA656FE4EAD0557A67D2F68C58C71C0EEFD80742062589DD46A29F1D3774A2C50C582B2E1239F362BF95C85944DA0F91D13F478BE7DB2944C751ACD174957A46285C9124CFC7E0C86162E5015613A60E294C6958DB0E3; dLzRVomUNn7A80S=DBTW0xNrJNrK66vO9XrcKsa3Gnb6mycqELEieQgDlGpd_iyYqp8lfVeHcvYHTndN; dLzRVomUNn7A80T=5M4BnHElbY3M4S2IEmvRwo7InuGmM2PYJVzrGpqS8mZwFH_46eT9nRdHF3pnlDeaCpbmSao6M0pCBIfCVJnrBE7OXJYDhmO4RFQ0DlEqMm7U7Uc4voSpwkQUhkzUyaw94GyrKQVTJpyBa4q_xX9P60AuHCVUydpxc68cpb3lvocnrTMvBVrPo..Cdql0OI3ae79kY2AlWBBli16eyL_sp6HgKVIwh0VKM7tghUsP_BLGqLUDX3mL3EHNkp5FoeSBicAdvflatta.vHGF5pw5gYq3Py68Ofyv05BxsTPXbvBGILuBgphD6WmIcn6nwa0CuG_HMXapHOjhVHV6CgGjyIHzwTCTTwZuiI9ZAJIAZQPv3ZnwUHmghw093ItcPk9o4NzV";
    public static final String ORIGIN = "http://www.cipread.com";
    public static final String REFERER = "http://www.cipread.com/View/DatabaseContent.aspx?type=1&keyword=&queryConn=&searchword=&newkeyword=";
    public static final String URL_PRE = "http://www.cipread.com";


    @Test
    public void initCrawler() throws Exception {
        doCrawler();
    }

    public void doCrawler() throws Exception {
        int pageNum = 695;
        int totalPage = 852;
        for (int i = pageNum; i <= totalPage; i++) {
            String url = "http://www.cipread.com/View/DatabaseContent.aspx?type=1&keyword=&queryConn=&searchword=&newkeyword=";
            Map<String, Object> parameter = MapUtil.newHashMap(8);
            parameter.put("__VIEWSTATE", "/wEPDwUJMzczNTc1NTEzD2QWAgIDD2QWAgIBD2QWBAIBDxYCHgRUZXh0BVnlhbHkuLrmgqjmkJzntKLliLAgPGI+ODUxNDwvYj4g5p2hIOW9k+WJjeaYvuekuuesrCA8Yj4xPC9iPiDpobUg5YWxIDg1MiDpobUg5pi+56S6MS0xMOadoWQCBQ8PFgIeC1JlY29yZGNvdW50AsJCZGRkuQVPxxtIGmhWBieCRFRPJXjJ1e2Uq5WnuN1jzp0EQyk=");
            parameter.put("__EVENTVALIDATION", "/wEdAAnlMIFjiejG4hn5RCLZnZbGPQPbTx8k2uWKI+ddE82h8zi5F1JX6N/NhY7nnaKv44jS3dp+zg2LeIl+ohaMdtoFTFg6esSO7da5CYLCFzpb29AbJlN4bIkVceyt5KPmqKD3C6okIQmIk9NHDF0ydusw/kBUOUn+wHsjvX8OLaL8Sex4pBvn7tBgXaIiLObEACUv8TnY2moFO+V9uBG6n8DP3vOGlR5UWLa0H6peZxdQmQ==");
            parameter.put("__VIEWSTATEGENERATOR", "D95EAC2A");
            parameter.put("__EVENTARGUMENT", i);
            parameter.put("__EVENTTARGET", "ctrl_Search$aspNetPager");
            parameter.put("ctrl_Search$aspNetPager_input", i);
            parameter.put("ctrl_Search$aspNetPager", 1);
            parameter.put("ctrl_Search$hid_type", 1);
            Map<String, Object> headers = MapUtil.newHashMap(5);
            headers.put("Cookie", COOKIE);
            headers.put("User-Agent", UserAgentUtil.random());
            headers.put("Origin", ORIGIN);
            headers.put("Referer", REFERER);
            String result = HttpUtil.doPost(url, headers, parameter);
            int tryCount = 1;
            while (result.contains("暂无相关记录，请重新选择") && tryCount <= 5) {
                //睡眠2秒继续访问
                Thread.sleep(2000);
                result = HttpUtil.doPost(url, headers, parameter);
                tryCount++;
            }
            if (StrUtil.isNotBlank(result)) {
                Document htmlDoc = Jsoup.parse(result);
                Elements bookContentList = htmlDoc.select("#search-result dl");
                if (bookContentList != null && bookContentList.size() > 0) {
                    List<CxgBook> listBook = new ArrayList<>();
                    for (Element element : bookContentList) {
                        Element bodyElement = element.select("dt a").first();
                        String href = bodyElement.attr("href");
                        if (StrUtil.isNotBlank(href)&&href.contains("doi=")) {
                            String[] split = href.split("doi=");
                            String targetHref = URL_PRE + href;
                            CxgBook bookEntity = getBookEntity(targetHref);
                            bookEntity.setPkId(split[1]);
                            bookEntity.setIsSync(0);
                            bookEntity.setCreateTime(LocalDateTime.now());
                            bookEntity.setPressName("化学工业出版社");
                            bookEntity.setStatus(1);
                            bookEntity.setPressId(29);
                            log.info(bookEntity.getBookName());
                            listBook.add(bookEntity);
                        }


                    }
                    bookService.saveBatch(listBook);
                    log.info("第" + i + "页数据保存成功");
                    log.warn("化工社--->" + i);
                }
            }
        }
    }

    public CxgBook getBookEntity(String targetHref) throws Exception {
        CxgBook bookEntity = null;
        Map<String, Object> headers = MapUtil.newHashMap(5);
        headers.put("Cookie", COOKIE);
        headers.put("User-Agent", UserAgentUtil.random());
        headers.put("Origin", ORIGIN);
        headers.put("Referer", REFERER);
        String result = HttpUtil.doGet(targetHref, headers);
        int tryCount = 1;
        while (result.contains("页面没有找到") && tryCount <= 5) {
            //睡眠2秒继续访问
            Thread.sleep(2000);
            result = HttpUtil.doGet(targetHref, headers);
            tryCount++;
        }
        if (StrUtil.isNotBlank(result)) {
            bookEntity = new CxgBook();
            Document htmlDoc = Jsoup.parse(result);
            Element imgEle = htmlDoc.getElementById("zoom_images_thumb_img");
            bookEntity.setCover(URL_PRE+imgEle.attr("src"));
            Element nameEle = htmlDoc.select(".sgbName").first();
            bookEntity.setBookName(nameEle.text());
            Elements liEle = htmlDoc.select(".sgbMain .summary li");
            for (Element ele : liEle) {
                String text = ele.text();
                if (StrUtil.isNotBlank(text)) {
                    if (text.startsWith("【图书作者】")) {
                        String author = text.substring(6);
                        bookEntity.setAuthor(author);
                    } else if (text.startsWith("【ＩＳＢＮ】")) {
                        String isbn = text.substring(6);
                        bookEntity.setBookIsbn(isbn.replaceAll("-", "").replaceAll("—", ""));
                    } else if (text.startsWith("【出版时间】")) {
                        String pubStr = text.substring(6);
                        pubStr = pubStr + " 00:00:00";
                        DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
                        LocalDateTime localDateTime = LocalDateTime.parse(pubStr, dtf);
                        bookEntity.setPubDate(localDateTime);
                    }
                }
            }
            Element digEle = htmlDoc.select(".sgbMain .book-price .btn-book a").first();
            if(digEle!=null){
                bookEntity.setDigitalUri(URL_PRE+digEle.attr("href"));
            }

            Elements briefEle = htmlDoc.select(".singleDetail .sm");

            for (Element ele : briefEle) {
                String text = ele.text();
                if (text.startsWith("目 录")) {
                    bookEntity.setCatalog(text);
                } else {
                    bookEntity.setBrief(text);
                }
            }

        }
        return bookEntity;
    }

    /**
     * 判断一个字符串是否都为数字
     *
     * @param strNum
     * @return
     */
    public boolean isDigit(String strNum) {
        Pattern pattern = Pattern.compile("[0-9]{1,}");
        Matcher matcher = pattern.matcher((CharSequence) strNum);
        return matcher.matches();
    }

    /**
     * 截取数字
     *
     * @param content
     * @return
     */
    public String getNumbers(String content) {
        Pattern pattern = Pattern.compile("\\d+");
        Matcher matcher = pattern.matcher(content);
        while (matcher.find()) {
            return matcher.group(0);
        }
        return "";
    }
}
